package com.helloworld.common.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.helloworld.common.dao.AbstractResultSetCallBack;
import com.helloworld.common.dao.SimpleBizDao;

@SuppressWarnings("unchecked")
@Repository(value = "simpleBizDao")
public class SimpleBizDaoImpl extends SqlSessionDaoSupport implements SimpleBizDao {

	@Override
	public List<Map<String, Object>> list(String mapperId, Map<String, Object> param) {
		return (List<Map<String, Object>>)getSqlSession().selectList(mapperId, param);
	}

	@Override
	public List<Map<String, Object>> listSQL(String sql) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("sql", sql);
		return (List<Map<String, Object>>)getSqlSession().selectList("common.sql.select", param);
	}
	
	@Override
	public List<Map<String, Object>> list(String mapperId, String param) {
		return (List<Map<String, Object>>)getSqlSession().selectList(mapperId, param);
	}

	@Override
	public Map<String, Object> select(String mapperId, Map<String, Object> param) {
		return (Map<String, Object>)getSqlSession().selectOne(mapperId, param);
	}
	
	@Override
	public Map<String, Object> selectSQL(String sql) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("sql", sql);		
		return (Map<String, Object>)getSqlSession().selectOne("common.sql.select", param);
	}
	
	@Override
	public Map<String, Object> select(String mapperId, String param) {
		return (Map<String, Object>)getSqlSession().selectOne(mapperId, param);
	}	
	
	@Override
	public String newSeq(String mapperId) {
		return (String)getSqlSession().selectOne(mapperId);
	}

	@Override
	public int insert(String mapperId, Map<String, Object> param) {
		return getSqlSession().insert(mapperId, param);
	}
	
	@Override
	public int insertSQL(String sql) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("sql", sql);		
		return getSqlSession().insert("common.sql.insert", param);
	}
	
	@Override
	public int update(String mapperId, Map<String, Object> param) {
		return getSqlSession().update(mapperId, param);
	}

	@Override
	public int updateSQL(String sql) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("sql", sql);		
		return getSqlSession().update("common.sql.update", param);
	}	

	@Override
	public int delete(String mapperId, Map<String, Object> param) {
		return getSqlSession().delete(mapperId, param);
	}
	
	@Override
	public int deleteSQL(String sql) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("sql", sql);		
		return getSqlSession().delete("common.sql.delete", param);
	}

	@Override
	public void listJdbcSql(String sql, AbstractResultSetCallBack callback) {
		Connection cn = null;
		Statement st = null;
		ResultSet rs = null;
		
		try {
			cn = getSqlSession().getConfiguration().getEnvironment().getDataSource().getConnection();
			st = cn.createStatement();
			rs = st.executeQuery(sql);
			callback.callback(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) try { rs.close(); } catch (Exception e) { e.printStackTrace(); }
			if (st != null) try { st.close(); } catch (Exception e) { e.printStackTrace(); }
			if (cn != null) try { cn.close(); } catch (Exception e) { e.printStackTrace(); }
		}
	}	

}
